package com.drd.dt.back.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drd.dt.util.ResultUtil;
import com.drd.dt.back.dao.DataReportMapper;
import com.drd.dt.back.service.IDataReportService;
import com.drd.dt.common.SuccessResultEnum;
import com.drd.dt.common.tips.Tip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 86514 on 2019/4/10.
 */
@Transactional
@Service("dataReportService")
public class DataReportServiceImpl implements IDataReportService {

    @Autowired
    private DataReportMapper dataReportMapper;

    /**
     * 数据报表信息统计-渠道汇总
     **/
    @Override
    public Tip channelList(Map<String, String> map) {
        List<Map> data = dataReportMapper.selectChannelDataList(map);
        Map total = getTotal(data);
        total.put("channel_id","渠道汇总");
        data.add(total);
        return ResultUtil.result(SuccessResultEnum.SUCCESS.getCode(), SuccessResultEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 数据报表信息统计-平台汇总
     **/
    @Override
    public Tip terraceList(Map<String, String> map) {
        List<Map> data = dataReportMapper.selectTerraceDataList(map);
        Map total = getTotal(data);
        total.put("type","平台汇总");
        data.add(total);
        return ResultUtil.result(SuccessResultEnum.SUCCESS.getCode(), SuccessResultEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 数据报表信息统计-订单收益汇总
     **/
    @Override
    public Tip incomeList(Map<String, String> map) {
        JSONArray arr = new JSONArray();
        List<Map> peopleInfo = dataReportMapper.peopleInfoList(map);//总注册人数
        List<Map> orderInfo = dataReportMapper.incomeList(map);//订单总佣金
        List<Map> buyInfo = dataReportMapper.buyInfoList(map);//订单购买人数
        peopleInfo.stream().forEach(t ->{
            JSONObject result = new JSONObject();
            String channel_id = String.valueOf(t.get("channel_id"));
            String peopleNum = String.valueOf(t.get("peopleNum"));
            result.put("channel_id",channel_id);
            result.put("peopleNum",peopleNum);
            orderInfo.stream().forEach(o ->{
                String order_channel_id = String.valueOf(o.get("channel_id"));
                String totalMoney = String.valueOf(o.get("totalMoney"));
                if (order_channel_id.equals(channel_id)){
                    result.put("money",totalMoney);
                }
            });
            buyInfo.stream().forEach(b ->{
                String order_channel_id = String.valueOf(b.get("channel_id"));
                String buyNum = String.valueOf(b.get("buyNum"));
                if (order_channel_id.equals(channel_id)){
                    result.put("buyNum",buyNum);
                }
            });

            Double money = result.getDouble("money");
            Integer buyNum = result.getInteger("buyNum");
            Double ave = 0.0;//人均佣金
            if (null != buyNum && buyNum > 0){
                ave = money / buyNum;
            }
            result.put("ave",String.format("%.2f",ave));
            arr.add(result);
        });

        return ResultUtil.result(SuccessResultEnum.SUCCESS.getCode(), SuccessResultEnum.SUCCESS.getMessage(), arr);
    }

    /**
     * 数据报表信息统计-自营订单收益汇总
     **/
    @Override
    public Tip ownOrderList(Map<String, String> map) {
        List<Map> data = dataReportMapper.selectOwnOrderList(map);
        return ResultUtil.result(SuccessResultEnum.SUCCESS.getCode(), SuccessResultEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 计算汇总
     * @return
     */
    private static Map getTotal(List<Map> data){
        Map total = new HashMap<>();
        Integer total_orderNum = 0;
        Double total_rake_back = 0.0;
        Double total_user_rake_back = 0.0;
        for (Map t:data){
            Integer orderNum = Integer.valueOf(t.get("orderNum").toString());
            Double rake_back = Double.valueOf(t.get("rake_back").toString());
            Double user_rake_back = Double.valueOf(t.get("user_rake_back").toString());
            total_orderNum = total_orderNum + orderNum;
            total_rake_back = rake_back + total_rake_back;
            total_user_rake_back = user_rake_back + total_user_rake_back;
        }
        total.put("orderNum",total_orderNum);
        total.put("rake_back",String.format("%.2f",total_rake_back));
        total.put("user_rake_back",String.format("%.2f",total_user_rake_back));
        return total;
    }
}
